import Http from '@/services/api/index';
const Behavior = require('@/behaviors/common'); // --混入
const Util = require('@/utils/util.js')
const Storage = require('@/utils/storage.js')
const Toast = require('@/utils/toast')
const {login} = require('@/middleware/auth')
const appInstance=getApp();
const BASE_URL= appInstance.globalData.baseUrl;
Page({
  behaviors: [Behavior], // 引入Behavior
  data: {baseUrl: BASE_URL,
    items: [{
        id: 1,
        image: './../../assets/images/banner2-01.png',
        scale: 0.9
      },
      {
        id: 2,
        image: './../../assets/images/banner2-01.png',
        scale: 0.9
      },
      {
        id: 3,
        image: './../../assets/images/banner2-01.png',
        scale: 0.9
      }
    ],
    currentIndex: 0,
    swiping: false
  },
  async onLoad() {
    await Http.get('/api/home/banner', {}, {
      isLoading: true
    }).then((res) => {
    }).catch(err => {
      console.error(err)
    })
  },
  async getCart() {
    await Http.get('/api/cart/get', {}, {
      skipAuth: false,
      isLoading: true
    }).then((res) => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
  },
  async verifyToken(e) {
    const {
      type
    } = e.currentTarget.dataset;
    const access_token = Storage.get('access_token');
    const refresh_token = Storage.get('refresh_token');

    await Http.post('/api/token/verify', {
      token: type == 0 ? access_token : refresh_token
    }, {
      skipAuth: true,
      isLoading: true
    }).then((res) => {
      console.log(res)
    }).catch(err => {
      console.error(err)
    })
  },
  // 获取用户信息(新API)
  async getUserProfile() {
    wx.login({
      success: async (res) => {
        if (res.code) {
          try {
            // 调用登录接口
            await Http.post('/api/auth/login', {
              code: res.code
            }, {
              header: {
                'Content-Type': 'application/json' // 明确指定JSON格式
              },
              skipAuth: true // 可选：跳过自动添加token
            }).then(res => {
              console.log("调用登录接口返回数据", res)
              if (res.data.code === 200) {
                // 存储token和用户信息
                let access_expires_in = Util.timeToSeconds(res.data.access_expires_in);
                let refresh_expires_in = Util.timeToSeconds(res.data.refresh_expires_in);
                console.log(access_expires_in, refresh_expires_in)
                Storage.set('access_token', res.data.access_token, 5 * 60); // --请求token设置5分钟
                Storage.set('refresh_token', res.data.refresh_token, refresh_expires_in || 1 * 60 * 60); // --刷新token设1小时
                Storage.set('userInfo', res.data.userInfo, refresh_expires_in || 1 * 60 * 60);
                // wx.setStorageSync('access_token', loginRes.data.access_token);
                // wx.setStorageSync('refresh_token', loginRes.data.refresh_token);
                // wx.setStorageSync('userInfo', loginRes.data.userInfo);
              } else {}
            }).catch(err => {
              console.log(err)
            })
          } catch (error) {
            console.error(error)
          }
        } 
      },
      fail: (err) => {}
    });
  },
  async refreshToken() {
    let refresh_token = Storage.get('refresh_token')
    await Http.post('/api/auth/refresh', {
      refresh_token: refresh_token
    }, {
      header: {
        'Content-Type': 'application/json' // 明确指定JSON格式
      },
      skipAuth: false // 可选：跳过自动添加token
    }).then((res) => {
      console.log(res)
      Storage.set('access_token', res.data.access_token, 5 * 60); // --请求token设置5分钟
    }).catch((err) => {
      console.log(err)
    })
  }
});